package com.sun.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.dao.UserMapper;
import com.sun.entity.User;
import com.sun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	/**
	 * 单个查询id
	 * @param id
	 * @return
	 * @throws Exception
	 * 没有使用通用mapper
	 */
	@Override
	public User selectUserById(Long id) throws Exception {
			return userMapper.selectUserById(id);
	}



	@Override
	public List<User> getUserList() throws Exception {
		return userMapper.getUserList();
	}

	@Override
	public User updateByid(Long id, User updatedUser) throws Exception {
		// 从数据库中获取当前用户信息
		User existingUser = userMapper.selectUserById(id);
		if (existingUser == null) {
			throw new RuntimeException("找不到用户id: " + id);
		}
		// 更新用户信息
		existingUser.setName(updatedUser.getName());
		existingUser.setAge(updatedUser.getAge());
		// 保存到数据库
		userMapper.updateByid(existingUser);
		// 返回更新后的用户信息
		return  existingUser;
	}

	@Override
	public void deleteByid(Long id) {
		userMapper.deleteByid(id);
	}

	/*使用通用Mapper*/
	@Override
	@Transactional
	public int updateUser(User user) {
		return userMapper.updateByPrimaryKeySelective(user);
	}

	@Override
	@Transactional
	public int addUser(User user) {
		return userMapper.insertSelective(user);
	}

	/**
	 * 登录处理
	 *
	 * @param
	 * @return
	 */
	public User login(String username, String password) {

		return userMapper.login(username,password);
	}

	/**
	 * 查看用户信息
	 * @param id
	 * @return
	 */
	@Override
	public User showDetail(Long id) {
		return userMapper.selectByPrimaryKey(id);
	}

	/**
	 * 删除单个用户
	 *
	 * @param id
	 * @return
	 */
	@Override
	@Transactional
	public int userDelByid(Long id) {
		return userMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 登录处理
	 *
	 * @param
	 * @param
	 * @return 登录成功返回用户信息和null，否则返回包含提示信息的Map
	 */
	/*public Map<String, Object> login(String username, String password) {
		Map<String, Object> result = new HashMap<>();

		// 查询数据库中是否存在该用户名
		User dbUser = userMapper.login(username);

		if (dbUser == null) {
			result.put("user", null);
			result.put("msg", "账号不存在！");
			return result;
		}

		// 验证密码是否正确
		if (!dbUser.getPassword().equals(password)) {
			result.put("user", null);
			result.put("msg", "密码错误！");
			return result;
		}

		// 登录成功
		result.put("user", dbUser);
		result.put("msg", null);
		return result;
	}*/

	public PageInfo<User> getUserByPage(int pageNum ,int pageSize){
		PageHelper.startPage(pageNum,pageSize);
		List<User> userList = userMapper.selectAll();
		return new PageInfo<>(userList);
	}



	// 搜索用户（分页）
	public PageInfo<User> searchUsersPage(String searchTerm, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize); // 关键：必须在查询前调用
		List<User> users = userMapper.searchUsersByIdNameSex(searchTerm); // 调用 Mapper 原方法
		return new PageInfo<>(users);
	}

	@Override
	public User selectUserByname(String username) {
		return userMapper.selectUserByname(username);
	}

	// 批量删除
	@Override
	@Transactional
	public int batchDeleteUsers(List<Long> ids) {
		if(ids == null || ids.isEmpty()){
			return 0;
		}
		return userMapper.batchDelete(ids);
	}


}